package com.quantumoctopus.magiceatball;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.URL;

import org.apache.commons.io.IOUtils;

import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

public class JSONWorker {

	// returns JSON object from a URL. Don't forget to sign the URL first
	public static String getJSONString(String u) throws Exception {

		URL url = new URL(u);
		InputStream is = (InputStream) url.getContent();
		String jsonText = IOUtils.toString(is, "UTF-8");
		return jsonText;
	}

	public static JsonElement getJSONElementFromString(String jsonText)
			throws Exception {
		JsonElement jse = new JsonParser().parse(jsonText);
		return jse;
	}

	// I guess this isn't really Json related, but whatever
	public static String readFile(String fileName) throws IOException {

		StringWriter stringWriter = new StringWriter();
		IOUtils.copy(new FileInputStream(new File(fileName)), stringWriter);
		return stringWriter.toString();
	}

}
